package com.shm.gbit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

/**
 * @author SHM
 * 查找两个字符串之间的最小距离
 */
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String ns = input.nextLine();
        int n = Integer.parseInt(ns);
        String str = input.nextLine();
        String[] words = str.split(" ");
        HashMap<String, ArrayList<Integer>> map = new HashMap<>();
        for (int i = 0; i < words.length; i++) {
            if (map.containsKey(words[i])) {
                map.get(words[i]).add(i);
            }else {
                ArrayList<Integer> list = new ArrayList<>();
                list.add(i);
                map.put(words[i],list);
            }
        }
        String word1 = input.nextLine();
        String word2 = input.nextLine();
        ArrayList<Integer> list1 = map.get(word1);
        ArrayList<Integer> list2 = map.get(word2);
        int minLen = Integer.MAX_VALUE;
        if (word1.equals(word2)){
            for (int i = 0; i < list1.size(); i++) {
                for (int j = i+1; j < list2.size(); j++) {
                    if (Math.abs(list1.get(i) - list2.get(j)) < minLen) {
                        minLen = Math.abs(list1.get(i) - list2.get(j));
                    }
                }
            }
        }else {
            for (int i = 0; i < list1.size(); i++) {
                for (int j = 0; j < list2.size(); j++) {
                    if (Math.abs(list1.get(i) - list2.get(j)) < minLen) {
                        minLen = Math.abs(list1.get(i) - list2.get(j));
                    }
                }
            }
        }
        System.out.println(minLen);
    }
}
